package com.jsu.springboot_medicine.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jsu.springboot_medicine.dao.UserDao;
import com.jsu.springboot_medicine.entity.User;
import com.jsu.springboot_medicine.service.UserService;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author js
 * @name UserServiceImpl
 * @Date 2023/9/25 15:42
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserDao userDao;
    @Resource
    private PasswordEncoder passwordEncoder;
    //每页显示多条数据
    private  Integer pageSize = 30;
    @Override
    public List<User> selectAll() {
        return userDao.selectAll();
    }

    @Override
    public User selectByUserName(String userName) {
        return userDao.selectByUserName(userName);
    }

    @Override
    public User selectById(int id) {
        return userDao.selectById(id);
    }

    @Override
    public int updateByPrimaryKeySelective(User user) {
        String password = user.getPassword();
        if (password!=null){
            String encode = passwordEncoder.encode(password);
            user.setPassword(encode);
        }
        user.setUpdateTime(new Date());
        return userDao.updateByPrimaryKeySelective(user);
    }

    @Override
    public List<User> selectByName(String name) {
        return userDao.selectByName(name);
    }

    @Override
    public int deleteByPrimaryKey(String userName) {
        return userDao.deleteByPrimaryKey(userName);
    }

    @Override
    public void add(User user) {
        String password = user.getPassword();
        String encodePassword = passwordEncoder.encode(password);
        user.setPassword(encodePassword);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        userDao.insertSelective(user);
    }

    @Override
    public PageInfo<User> findPage(Integer pageNum) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> users = userDao.selectAll();
        PageInfo<User> userPageInfo = new PageInfo<User>(users);
        return userPageInfo;
    }

    @Override
    public PageInfo<User> findPage(User user, Integer pageNum, Integer pageSize) {
        List<User> users = userDao.selectAll();
        PageInfo<User> pageInfo = new PageInfo<User>(users);
        return pageInfo;
    }
}
